#include <vector>
#include <string>
#include <iostream>

using std::cout;
using std::endl;
using std::string;

//#define MAX 1000
//      模板参数推导
//函数模板 --> 模板函数
//类模板 --->  模板类
template <class T, int Size = 10>
class Stack
{
public:
    Stack()
    : _top(-1)
    , _base(new T[Size]())
    {}

    ~Stack();

    bool empty() const;
    bool full() const;
    void push(const T & t);
    void pop();
    T top() const;

private:
    int _top;
    T * _base;
};

template <class T, int Size>
Stack<T, Size>::~Stack()
{
    if(_base) {
        delete [] _base;
        _base = nullptr;
    }
}

template <class T, int Size>
bool Stack<T, Size>::empty() const
{   return _top == -1;  }

template <class T, int Size>
bool Stack<T, Size>::full() const
{   return _top == Size - 1;  }

template <class T, int Size>
void Stack<T, Size>::push(const T & t)
{
    if(!full()) {
        _base[++_top] = t;
    } else {
        cout << "stack is full, cannot push any more data." << endl;
    }
}

template <class T, int Size>
void Stack<T, Size>::pop()
{
    if(!empty()) {
        --_top;
    } else {
        cout << "stack is empty, no more data." << endl;
    }
}

//没有在函数内进行判断，就要求每次获取栈顶元素之前，应该先判断是否有元素
template <class T, int Size>
T Stack<T, Size>::top() const
{
    return _base[_top];
}

void test0()
{
    Stack<int> stack;
    cout << "stack是否为空?" << stack.empty() << endl;
    stack.push(1);
    cout << "stack是否为空?" << stack.empty() << endl;
    for(int i = 2; i < 12; ++ i) {
        stack.push(i);
    }
    cout << "stack是否已满?" << stack.full() << endl;

    while(!stack.empty()) {
        cout << stack.top() << endl;
        stack.pop();
    }
    cout << "stack是否已满?" << stack.full() << endl;
    cout << "stack是否为空?" << stack.empty() << endl;
}

void test1()
{
    Stack<string> stack;
    cout << "stack是否为空?" << stack.empty() << endl;
    stack.push(string(3, 'a'));
    cout << "stack是否为空?" << stack.empty() << endl;
    for(int i = 1; i < 12; ++ i) {
        stack.push(string(3, 'a' + i));
    }
    cout << "stack是否已满?" << stack.full() << endl;

    while(!stack.empty()) {
        cout << stack.top() << endl;
        stack.pop();
    }
    cout << "stack是否已满?" << stack.full() << endl;
    cout << "stack是否为空?" << stack.empty() << endl;

}


int main()
{
    //std::vector<int> vec;
    /* test0(); */
    test1();

    return 0;
}

